using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using SigmaStock_Components;

namespace project
{
    public partial class NewStaff : Form
    {
        string staff_name;
        string address1, address2;
        string phone, email;
        string query;
        OleDbConnection conn;
        public NewStaff()
        {
            InitializeComponent();
            textBox1.ReadOnly = false;
            dbconnection();
        }

        private void dbconnection()
        {
            conn = DBComponent.Mag_DealerDB.dbconnection();
            conn.Open();

        }


        private void NewStaff_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'mag_dealerDataSet.staff' table. You can move, or remove it, as needed.
            this.staffTableAdapter.Fill(this.mag_dealerDataSet.staff);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (ValidateStaffDetails())
            {
                staff_name = textBox1.Text;
                address1 = richTextBox1.Text;
                address2 = richTextBox2.Text;
                int phone = int.Parse(textBox2.Text);
                int salary = int.Parse(textBox5.Text);
                email = textBox3.Text;
                DateTime app_date = dateTimePicker1.Value;
                DateTime doj = dateTimePicker2.Value;
                query="insert into staff values('"+staff_name+"','"+address1+"','"+address2+"','"+phone+"','"+email+"','"+app_date.ToString()+"','"+doj.ToString()+"','"+salary+"');";
                OleDbCommand command = new OleDbCommand(query, conn);
                command.ExecuteNonQuery();
                MessageBox.Show("Data Inserted Successfully !!!");
                UpdateDataGrid();
            }
        }

        private void UpdateDataGrid()
        {
            String s;
            s = "Select * from staff";
            OleDbCommand cmd1 = new OleDbCommand(s, conn);
            OleDbDataAdapter da = new OleDbDataAdapter(s, conn);
            DataSet ds = new DataSet();
            da.Fill(ds, "staff");
            staffDataGridView.DataSource = ds;
            staffDataGridView.DataMember = "staff";
            staffBindingSource.ResetBindings(false);
            this.Refresh();
        }

        private bool ValidateStaffDetails()
        {
            textBox6.Visible = true;
            if (String.IsNullOrEmpty(textBox1.Text) || !(Validation.IsAlpha(textBox1.Text)))
            {
                textBox6.Text = "Check Employee Name";
                Console.Beep();
                return false;
            }
            if ((String.IsNullOrEmpty(richTextBox1.Text)))
            {
                textBox6.Text = "Check Address1";
                Console.Beep();
                return false;
            }

            if ((String.IsNullOrEmpty(richTextBox2.Text)))
            {
                textBox6.Text = "Check Address2";
                Console.Beep();
                return false;
            }

            if ((String.IsNullOrEmpty(textBox2.Text)) || !(Validation.IsNumber(textBox2.Text)))
            {
                textBox6.Text = "Check Contact no";
                Console.Beep();
                return false;
            }
            if ((String.IsNullOrEmpty(textBox3.Text)))
            {
                textBox6.Text = "Check email id";
                Console.Beep();
                return false;
            }

            if ((String.IsNullOrEmpty(textBox5.Text)) || !(Validation.IsNumber(textBox5.Text)))
            {
                textBox6.Text = "Check Salary";
                Console.Beep();
                return false;
            }
            return true;
        }

        private void staffBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.staffBindingSource.EndEdit();
            this.staffTableAdapter.Update(this.mag_dealerDataSet.staff);

        }

 
        private void button2_Click(object sender, EventArgs e)
        {
            if (ValidateStaffDetails())
            {
                staff_name = textBox1.Text;
                address1 = richTextBox1.Text;
                address2 = richTextBox2.Text;
                int phone = int.Parse(textBox2.Text);
                email = textBox3.Text;
                int salary = int.Parse(textBox5.Text);
                DateTime app_date = dateTimePicker1.Value;
                DateTime doj = dateTimePicker2.Value;
                try
                {
                    string sqlquery = "UPDATE staff set ADDRESS1='" + address1 + "',ADDRESS2='" + address2 + "',PHONENO='" + phone + "',EMAIL='" + email + "',APPOINTMENT_DATE='" + app_date.ToString() + "',DATE_OF_JOIN='" + doj.ToString() + "',SALARY='" + salary + "' where ENAME='" + staff_name + "';";
                    OleDbCommand cmd = new OleDbCommand(sqlquery, conn);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Data Modified Successfully !!!");
                    UpdateDataGrid();
                }
                catch (Exception exc)
                {

                }
            }
        }

        private void NewStaff_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (Application.OpenForms.Count == 1)
            {
                Program.main_form.Show();
            }
        }

        private void staffDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                DataGridViewRowCollection datarows = staffDataGridView.Rows;
                DataGridViewRow selrow = datarows[e.RowIndex];
                selrow.Selected = true;
                foreach (DataGridViewRow dr in datarows)
                {
                    if (dr.Selected)
                    {
                        textBox1.ReadOnly = true;
                        textBox1.Text = dr.Cells[0].Value.ToString();
                        richTextBox1.Text = dr.Cells[1].Value.ToString();
                        richTextBox2.Text = dr.Cells[2].Value.ToString();
                        textBox2.Text = dr.Cells[3].Value.ToString();
                        textBox3.Text = dr.Cells[4].Value.ToString();
                        dateTimePicker1.Value = DateTime.Parse(dr.Cells[5].Value.ToString());
                        dateTimePicker2.Value = DateTime.Parse(dr.Cells[6].Value.ToString());
                        textBox5.Text = dr.Cells[7].Value.ToString();

                    }

                }
            }
            catch (Exception ex) { 
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.ReadOnly = false;
            textBox1.ResetText();
            textBox2.ResetText();
            textBox3.ResetText();
            textBox5.ResetText();
            richTextBox1.ResetText();
            richTextBox2.ResetText();
        }
    }
}